HTMLify
Sum of bit differences.java
Views: 1 | Author: cody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // Sum of bit differences java solution brute force followed by optimal import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int tc = Integer.parseInt(br.readLine()); while (tc-- > 0) { int n = Integer.parseInt(br.readLine()); int[] arr = new int[n]; String[] inputLine = br.readLine().split(" "); for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(inputLine[i]); } System.out.println(new Solution().sumBitDifferences(arr, n)); } } } // } Driver Code Ends //User function Template for Java // class Solution { // long sumBitDifferences(int[] arr, int n) { // // code here // long ans=0; // for(int i=0 ; i<n-1 ; i++){ // for(int j=i+1 ; j<n ; j++){ // ans+=2*Integer.bitCount(arr[i]^arr[j]); // } // } // return ans; // } // } class Solution { long sumBitDifferences(int[] arr, int n) { long ans = 0; for (int i = 0; i < 32; i++) { int setBitsCount = 0; for (int j = 0; j < n; j++) { setBitsCount += (arr[j] >> i) & 1; } ans += (long) setBitsCount * (n - setBitsCount) * 2; } return ans; } } |